package demo.c2;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * FightQueryTask
 *
 * @author chisheng
 */
public class FightQueryTask extends Thread implements FightQuery {

    /**
     * Original place
     */
    private final String origin;

    /**
     * Destination
     */
    private final String destination;

    /**
     * Fight information list
     */
    private final List<String> fightList = new ArrayList<>();

    public FightQueryTask(String airline, String origin, String destination) {
        super("[" + airline + "]");
        this.origin = origin;
        this.destination = destination;
    }

    @Override
    @SneakyThrows
    public void run() {
        System.out.printf("%s-query from %s to %s \n", this.getName(), origin, destination);
        final int randomVal = ThreadLocalRandom.current().nextInt(10);
        TimeUnit.SECONDS.sleep(randomVal);
        this.fightList.add(this.getName() + "-" + randomVal);
        System.out.printf("The fight: %s list query successfully.\n", this.getName());
    }

    @Override
    public List<String> get() {
        return this.fightList;
    }

}
